﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using GoToCoord.Models;
using System.Collections.Generic;
using GoToCoord.Utils;
using GoToCoord.Services;
using System.Device.Location;

namespace GoToCoord.ViewModels
{
    [SurviveTombstone]
    public class ChangeCoordinatesViewModel : PropertyChangedBase
    {
        private readonly INavigationService navigationService;
        private readonly IEventAggregator eventAggregator;
        private readonly ParameterService parameterService;

        private ViewMode selectedViewMode = ViewMode.Decimal;
        [SurviveTombstone]
        public ViewMode SelectedViewMode
        {
            get { return selectedViewMode; }
            set
            {
                if (selectedViewMode != value)
                {
                    selectedViewMode = value;
                    NotifyOfPropertyChange(() => SelectedViewMode);
                    NotifyOfPropertyChange(() => DecimalModeVisible);
                    NotifyOfPropertyChange(() => HourModeVisible);
                }
            }
        }

        public IEnumerable<ViewMode> ViewModes
        {
            get { return ViewMode.Modes; }
        }

        [SurviveTombstone]
        public string LatHours { get; set; }
        [SurviveTombstone]
        public string LatMinutes { get; set; }

        [SurviveTombstone]
        public string LongHours { get; set; }
        [SurviveTombstone]
        public string LongMinutes { get; set; }

        public bool DecimalModeVisible
        {
            get { return selectedViewMode == ViewMode.Decimal; }
        }

        public bool HourModeVisible
        {
            get { return selectedViewMode == ViewMode.Hour; }
        }

        public void AddCoordButton()
        {
            if (selectedViewMode == ViewMode.Decimal)
            {
                SetDecimalValues();
            }
            else if (selectedViewMode == ViewMode.Hour)
            {
                SetHourValues();
            }
        }

        #region Kontruktor

        public ChangeCoordinatesViewModel(INavigationService navigationService, IEventAggregator eventAggregator, ParameterService parameterService)
        {
            this.navigationService = navigationService;
            this.eventAggregator = eventAggregator;
            this.parameterService = parameterService;

            /*
            LatHours = "53.171933";
            LongHours = "8.191266"; 
             */
        }

        #endregion

        private void SetDecimalValues()
        {
            double lat;
            double lon;
            bool latOk = Double.TryParse(LatHours, out lat);
            bool longOk = Double.TryParse(LongHours, out lon);

            if (latOk && longOk)
            {
                parameterService.Push(new GeoCoordinate(lat, lon));
                navigationService.GoBack();
            }
        }

        private void SetHourValues()
        {
            double latHours;
            double longHours;
            double latMinutes;
            double longMinutes;

            bool latHoursOk = Double.TryParse(LatHours, out latHours);
            bool longHoursOk = Double.TryParse(LongHours, out longHours);
            bool latMinutesOk = Double.TryParse(LatMinutes, out latMinutes);
            bool longMinutesOk = Double.TryParse(LongMinutes, out longMinutes);

            if (latHoursOk && longHoursOk &&
                latMinutesOk && longMinutesOk)
            {
                double lat = latHours + latMinutes / 60;
                double lon = longHours + longMinutes / 60;

                parameterService.Push(new GeoCoordinate(lat, lon));
                navigationService.GoBack();
            }
        }
    }
}
